using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Sandia.MVCFramework.Plugin
{
    /// <summary>
    /// A form used to display file synchronization progress information.
    /// </summary>
    public partial class SynchronizationProgressBarForm : Form
    {
        /// <summary>
        /// The name of the file currently being synchronized.
        /// </summary>
        private string fileSynchronizeName = "";

        /// <summary>
        /// The current total progress of the synchronization.
        /// </summary>
        private int totalProgress = 0;

        /// <summary>
        /// The progress on the file that is currently being synchronized.
        /// </summary>
        private int fileSyncProgress = 0;

        /// <summary>
        /// The name of the file currently being synchronized.
        /// </summary>
        public string FileSynchronizeName
        {
            set
            {
                if (this.InvokeRequired)
                {
                    try
                    {
                        this.fileSynchronizeName = value;
                        this.BeginInvoke(new MethodInvoker(setFileSynchronizeName));
                    }
                    catch (Exception e)
                    {
                        // Use exception handling policy to determine what to do
                        bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                        if (rethrow)
                        {
                            throw;
                        }
                    }
                }
                else
                {

                    try
                    {
                        this.fileSynchronizeName = value;
                        this.setTotalProgress();
                    }
                    catch (Exception e)
                    {
                        // Use exception handling policy to determine what to do
                        bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                        if (rethrow)
                        {
                            throw;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Sets the name of the file currently being synchronized.
        /// </summary>
        private void setFileSynchronizeName()
        {
            this.fileSyncLabel.Text = "Synchronizing File: " + this.fileSynchronizeName;
        }

        /// <summary>
        /// The current total progress of the synchronization.
        /// </summary>
        public int TotalSynchronizeProgress
        {
            set
            {
                if (this.InvokeRequired)
                {
                    try
                    {
                        this.totalProgress = value;
                        // If so, call invoke on this method 
                        this.BeginInvoke(new MethodInvoker(setTotalProgress));
                    }
                    catch (Exception e)
                    {
                        // Use exception handling policy to determine what to do
                        bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                        if (rethrow)
                        {
                            throw;
                        }
                    }
                }
                else
                {

                    try
                    {
                        this.totalProgress = value;
                        this.setTotalProgress();
                    }
                    catch (Exception e)
                    {
                        // Use exception handling policy to determine what to do
                        bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                        if (rethrow)
                        {
                            throw;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Sets the current total progress of the synchronization.
        /// </summary>
        private void setTotalProgress()
        {
            this.totalPB.Value = this.totalProgress;
            this.Text = "Synchronizing Files: " + this.totalProgress.ToString() + "%";
        }

        /// <summary>
        /// The progress on the file that is currently being synchronized.
        /// </summary>
        public int FileSynchronizeProgress
        {
            set
            {
                if (this.InvokeRequired)
                {
                    try
                    {
                        this.fileSyncProgress = value;
                        // If so, call invoke on this method 
                        this.BeginInvoke(new MethodInvoker(setFileSyncProgress));
                    }
                    catch (Exception e)
                    {
                        // Use exception handling policy to determine what to do
                        bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                        if (rethrow)
                        {
                            throw;
                        }
                    }
                }
                else
                {

                    try
                    {
                        this.fileSyncProgress = value;
                        this.setFileSyncProgress();
                    }
                    catch (Exception e)
                    {
                        // Use exception handling policy to determine what to do
                        bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                        if (rethrow)
                        {
                            throw;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Sets the progress on the file that is currently being synchronized.
        /// </summary>
        private void setFileSyncProgress()
        {
            this.fileSyncPB.Value = this.fileSyncProgress;
        }

        /// <summary>
        /// Constructor for SynchronizationProgressBarForm.
        /// </summary>
        public SynchronizationProgressBarForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Shows form with invoke required checking
        /// </summary>
        public void ShowWithInvokeCheck()
        {
            if (this.InvokeRequired)
            {
                try
                {
                    // If so, call invoke on this method 
                    this.BeginInvoke(new MethodInvoker(ShowWithInvokeCheck));
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
            else
            {
                try
                {
                    this.Show();
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
        }

        /// <summary>
        /// Hides form with invoke required checking
        /// </summary>
        public void HideWithInvokeCheck()
        {
            if (this.InvokeRequired)
            {
                try
                {
                    // If so, call invoke on this method 
                    this.BeginInvoke(new MethodInvoker(HideWithInvokeCheck));
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
            else
            {
                try
                {
                    this.Hide();
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
        }
    }
}